<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;

class CashLog extends CxhshopBase {

    protected $table = 'ims_cxh_cash_log';

    protected $fillable = ['cash_bus_type', 'cash_bus_id', 'cash_type', 'cash_now', 'cash_old', 'cash_num','remark', 'member_id', 'ctime'];

    public $timestamps = false;

    protected $dates = [];

    public static $rules = [
        // Validation rules
    ];
    public function addCashLog($memInfo=[],$userCashId=0,$params=[]){
        $credit2 = $params['credit2'];
        $credit4 = $params['credit4'];
        $credit5 = $params['credit5'];
        $idcard = $params['idcard'];
        $cashlogData = [
            'cash_bus_type'=>50,
            'cash_bus_id'=>$userCashId,
            'member_id'=> $memInfo['id'],
            'ctime'=> time(),
        ];
        $memberSaveData = [];
        //余额减少；
        if($credit2 > 0){
            $cashlogData['cash_type'] = 1;
            $cashlogData['cash_old'] = $memInfo['credit2'];
            $cashlogData['cash_now'] = $memInfo['credit2'] - $credit2;
            $cashlogData['cash_num'] = '-'.$credit2;
            $cashlogData['remark'] = '[会员提现]'.$credit2.'余额为冻结资金';
            $query = self::query();
            $cashLogRe2 = $query -> insertGetId($cashlogData);
            $memberSaveData['credit2'] = $memInfo['credit2'] - $credit2;
        }else{
            $cashLogRe2 = 1;
        }
        //里程金减少；
        if($credit4 > 0){
            $cashlogData['cash_type'] = 5;
            $cashlogData['cash_old'] = $memInfo['credit4'];
            $cashlogData['cash_now'] = $memInfo['credit4'] - $credit4;
            $cashlogData['cash_num'] = '-'.$credit4;
            $cashlogData['remark'] = '[会员提现]'.$credit4.'里程金为冻结资金';
            $query = self::query();
            $cashLogRe4 = $query -> insertGetId($cashlogData);
            $memberSaveData['credit4'] = $memInfo['credit4'] - $credit4;
        }else{
            $cashLogRe4 = 1;
        }
        //运营保证金减少；
        if($credit5 > 0){
            $cashlogData['cash_type'] = 6;
            $cashlogData['cash_old'] = $memInfo['credit5'];
            $cashlogData['cash_now'] = $memInfo['credit5'] - $credit5;
            $cashlogData['cash_num'] = '-'.$credit5;
            $cashlogData['remark'] = '[会员提现]'.$credit5.'运营保证金为冻结资金';
            $query = self::query();
            $cashLogRe5 = $query -> insertGetId($cashlogData);
            $memberSaveData['credit5'] = $memInfo['credit5'] - $credit5;
        }else{
            $cashLogRe5 = 1;
        }
        if(empty($memInfo['idcard'])){
            $memberSaveData['idcard'] = $idcard;
        }
        $returnData = [
            'cashLogRe2'=>$cashLogRe2,
            'cashLogRe4'=>$cashLogRe4,
            'cashLogRe5'=>$cashLogRe5,
            'memberSaveData'=>$memberSaveData,
        ];
        return $returnData;
    }

    /**
     * 获取财务中心其他记录 （cash_bus_type为54   cash_type为2）
     * params1 int memberid
     * params2 array params
     */
    public function getOtherRecord($memberid, $params) {

        $pagenNum = isset($params['page']) ? $params['page'] : 1;
        $limit = isset($params['limit']) ? $params['limit'] : 10;
        $page = $pagenNum - 1;
        $page = $limit * $page;

        if(isset($params['end'])) {
            $end = $params['end'];
        }

        $query = self::query();

        $query -> offset($page) -> limit($limit);
        $query -> orderBy('ctime', 'DESC');
        isset($end) && $query -> whereCtimelt($end);
        $query -> where('cash_bus_type', '=', '54'); 
        $query -> where('cash_type', '=', '1'); 
        $query -> where('member_id', '=', $memberid); 
        $list = $query -> get(['id', 'cash_bus_type', 'cash_bus_id', 'cash_type', 'cash_now', 'cash_old', 'cash_num', 'remark', 'ctime']) -> toArray();
   
        if($list) {

            foreach($list as $key => $value) {
                $time[] = date("Y-m",$value['ctime']);
                $list[$key]['ym'] = date("Y-m",$value['ctime']);
                $list[$key]['ctime'] = date("Y-m-d H:i:s",$value['ctime']);
            }

        }

        return $list;

    }

    /**
     * 获取我的积分记录
     * params1 int memberid
     * params2 array $params
     */
    public function getMyIntegral($memberid, $params) {

        $pagenNum = isset($params['page']) ? $params['page'] : 1;
        $limit = isset($params['limit']) ? $params['limit'] : 10;
        $page = $pagenNum - 1;
        $page = $limit * $page;

        if(isset($params['end'])) {
            if($params['end']) {
                $end = $params['end'];
            }
        }

        $query = self::query();
        $query -> offset($page) -> limit($limit);
        $query -> orderBy('ctime', 'DESC');

        isset($end) && $query -> whereCtimelt($end); 
        $list = $query -> where('cash_type', '=', '2') 
                       -> where('member_id', '=', $memberid) 
                       -> get(['cash_num', 'remark', 'ctime']) 
                       -> toArray();

        if($list) {
            
            foreach($list as $key => $value) {
                $time[] = date("Y-m",$value['ctime']);
                $list[$key]['ym'] = date("Y-m",$value['ctime']);
                $list[$key]['ctime'] = date("Y-m-d H:i:s",$value['ctime']);
            }

        }

        return $list;

    }

    /**
     * 查询条件 时间小于
     * params1 $query
     * params2 $time
     */
    public function scopeWhereCtimeLt($query, $time) {

        return $query -> where('ctime', '<', $time);

    }

}